package com.ajx.blog.service.impl;

import com.ajx.blog.entity.Blogusers;
import com.ajx.blog.mapper.BlogusersMapper;
import com.ajx.blog.service.api.BlogusersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BlogusersServiceImpl extends ServiceImpl<BlogusersMapper, Blogusers> implements BlogusersService {

    @Autowired
    private BlogusersMapper blogusersMapper;

    /**
     * @param {Blogusers} blogusers 待注册用户
     * @return {boolean} 是否添加成功 true/false
     * 
     * @description: 用户注册
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean insertOneUser(Blogusers blogusers) {
        QueryWrapper<Blogusers> qw = new QueryWrapper<>();
        qw.eq("uphone", blogusers.getUphone());
        if (blogusersMapper.selectOne(qw) != null) {
            return false;
        } else {
            return blogusersMapper.insert(blogusers) > 0;
        }
    }

    /**
     * @param {Integer} userId 用户ID
     * @return {boolean} 是否删除成功 true/false
     * 
     * @description: 根据用户ID删除一名用户
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean deleteOneUserById(Integer userId) {
        return blogusersMapper.deleteById(userId) > 0;
    }

    /**
     * @param {Integer} uid 博客用户ud
     * @return {boolean} 修改是否成功 true/false
     * 
     * @description: 修改博客用户状态
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean changeUserState(Integer uid) {
        Blogusers bu = blogusersMapper.selectById(uid);
        Blogusers nbu = new Blogusers();
        if (bu != null && "1".equals(bu.getUstatus())) {
            nbu.setUid(uid);
            nbu.setUstatus("-1");
            blogusersMapper.updateById(nbu);
            return true;
        } else if (bu != null && "-1".equals(bu.getUstatus())) {
            nbu.setUid(uid);
            nbu.setUstatus("1");
            blogusersMapper.updateById(nbu);
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param {Integer} page 当前页码数
     * @return {PageInfo<Blogusers>} 封装的分页信息对象
     * 
     * @description: 查询所有博客用户
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<Blogusers> selectAllUserByLimit(Integer page) {
        if (page != null) {
            QueryWrapper<Blogusers> qw = new QueryWrapper<>();
            PageHelper.startPage(page, 5);
            List<Blogusers> adminList = blogusersMapper.selectList(qw);
            PageInfo<Blogusers> info = new PageInfo<Blogusers>(adminList);
            return info;
        } else {
            return null;
        }
    }

    /**
     * @param {Integer} bloguid 博客用户ID
     * @return {Blogusers} 对应的博客用户对象
     * 
     * @description: 通过ID查询博客用户
     */
    @Transactional(readOnly = true)
    @Override
    public Blogusers selectBlogusers(Integer bloguid) {
        return blogusersMapper.selectById(bloguid);
    }

    /**
     * @param {String} uphone 用户手机号
     * @return {Blogusers} 对应的博客用户对象
     * 
     * @description: 通过手机号查询博客用户
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Blogusers selectBlogusersByPhone(String uphone) {
        QueryWrapper<Blogusers> qw = new QueryWrapper<>();
        qw.eq("uphone", uphone);
        return blogusersMapper.selectOne(qw);
    }

    /**
     * @param {*}
     * @return {Integer} 所有用户数量
     * 
     * @description: 查询所有用户数量
     */
    @Transactional(readOnly = true)
    @Cacheable(cacheNames = "selectBloguserCount", unless = "#result == null")
    @Override
    public Integer selectBloguserCount() {
        QueryWrapper<Blogusers> blogusersQW = new QueryWrapper<>();
        return blogusersMapper.selectCount(blogusersQW);
    }

}